/**
 *====================================================
 * 文件名称: BulkstockNoticeCastor.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年11月26日			chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.action.v1.dto;

import java.util.List;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.member.entity.LogisticsCompanyEntity;
import com.yuexunit.finance.member.service.LogisticsCompanyService;
import com.yuexunit.finance.project.action.v1.model.TransportNotice;
import com.yuexunit.finance.project.action.v1.model.output.DeliveryNoticeDetail;
import com.yuexunit.finance.project.action.v1.model.output.DeliveryNoticeLineInfo;
import com.yuexunit.finance.project.action.v1.model.output.RecieveNoticeDetail;
import com.yuexunit.finance.project.action.v1.model.output.RecieveNoticeInventory;
import com.yuexunit.finance.project.action.v1.model.output.RecieveNoticeLineInfo;
import com.yuexunit.finance.project.action.v1.model.output.SupervisionNoticeDetail;
import com.yuexunit.finance.project.action.v1.model.output.SupervisionNoticeLineInfo;
import com.yuexunit.finance.project.action.v1.model.output.TransportNoticeDetail;
import com.yuexunit.finance.project.action.v1.model.output.TransportNoticeLineInfo;
import com.yuexunit.finance.project.entity.ContainerBulkstockEntity;
import com.yuexunit.finance.project.entity.ContainerInventoryEntity;
import com.yuexunit.finance.project.entity.DeliveryNoticeEntity;
import com.yuexunit.finance.project.entity.FinanceApplicationEntity;
import com.yuexunit.finance.project.entity.FinanceBulkstockEntity;
import com.yuexunit.finance.project.entity.FinanceContractEntity;
import com.yuexunit.finance.project.entity.PickupApplicationEntity;
import com.yuexunit.finance.project.entity.PickupBulkstockEntity;
import com.yuexunit.finance.project.entity.RecieveNoticeEntity;
import com.yuexunit.finance.project.entity.RecieveNoticeInventoryEntity;
import com.yuexunit.finance.project.entity.SupervisionContractEntity;
import com.yuexunit.finance.project.entity.SupervisionNoticeEntity;
import com.yuexunit.finance.project.entity.TransportNoticeEntity;
import com.yuexunit.finance.project.entity.lexicon.BulkstockSourceEnum;
import com.yuexunit.finance.project.service.ContainerBulkstockService;
import com.yuexunit.finance.project.service.ContainerInventoryService;
import com.yuexunit.finance.project.service.FinanceApplicationService;
import com.yuexunit.finance.project.service.FinanceBulkstockService;
import com.yuexunit.finance.project.service.FinanceContractService;
import com.yuexunit.finance.project.service.PickupBulkstockService;
import com.yuexunit.finance.project.service.RecieveNoticeInventoryService;
import com.yuexunit.finance.project.service.SupervisionContractService;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: BulkstockNoticeCastor
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年11月26日 下午10:28:37
 */
public class BulkstockNoticeCastor {

	/** 监管通知 */
	public static SupervisionNoticeEntity fromSupervisionNotice(Long financeId, String noticeContent) {
		SupervisionNoticeEntity entity = new SupervisionNoticeEntity();
		entity.setFinanceId(financeId);
		entity.setNoticeContent(noticeContent);

		SupervisionContractService supervisionContractService = ComponentFactory.loadClass(SupervisionContractService.class);
		SupervisionContractEntity supervisionContract = supervisionContractService.inquireFinanceIdSupervisionContract(financeId);
		entity.setFinanceId(supervisionContract.getFinanceId());
		entity.setFinanceNumber(supervisionContract.getFinanceNumber());
		entity.setSupervisionContractId(supervisionContract.getSupervisionContractId());
		entity.setContractNumber(supervisionContract.getContractNumber());
		entity.setLogisticsCompanyId(supervisionContract.getLogisticsCompanyId());
		entity.setLogisticsCompanyName(supervisionContract.getLogisticsCompanyName());
		return entity;
	}

	public static SupervisionNoticeDetail toSupervisionNoticeDetail(SupervisionNoticeEntity entity) {
		SupervisionNoticeDetail detail = new SupervisionNoticeDetail();
		if (entity != null) {
			BeanUtils.copyProperties(entity, detail);
			detail.setNoticeState(entity.getNoticeState().ordinal());
		}
		return detail;
	}

	public static SupervisionNoticeLineInfo toSupervisionNoticeLineInfo(SupervisionNoticeEntity entity) {
		SupervisionNoticeLineInfo info = new SupervisionNoticeLineInfo();
		BeanUtils.copyProperties(entity, info);
		info.setNoticeState(entity.getNoticeState().ordinal());
		return info;
	}

	// =====================================================================================================================================================//
	/** 收货通知 */
	public static RecieveNoticeEntity fromRecieveNotice(FinanceApplicationEntity financeEntity, Long logisticsCompanyId, String logisticsCompanyName, String noticeRemark, Long[] bulkstockIds) {
		RecieveNoticeEntity recieveNoticeEntity = new RecieveNoticeEntity();
		BeanUtils.copyProperties(financeEntity, recieveNoticeEntity);
		recieveNoticeEntity.setLogisticsCompanyId(logisticsCompanyId);
		recieveNoticeEntity.setLogisticsCompanyName(logisticsCompanyName);
		recieveNoticeEntity.setNoticeRemark(noticeRemark);
		recieveNoticeEntity.setProductType(financeEntity.getProductType());
		// 验证货源一致
		FinanceBulkstockService financeBulkstockService = ComponentFactory.loadClass(FinanceBulkstockService.class);
		BulkstockSourceEnum bulkstockSource = null;
		Long exchangePickupId = null;
		Long replenishCreditId = null;
		for (int i = 0; i < bulkstockIds.length; i++) {
			FinanceBulkstockEntity fbe = financeBulkstockService.inquireFinanceBulkstock(bulkstockIds[i]);
			if (bulkstockSource == null) {
				bulkstockSource = fbe.getBulkstockSource();
				replenishCreditId = fbe.getReplenishCreditId();
				exchangePickupId = fbe.getExchangePickupId();
			} else if (BulkstockSourceEnum.FINANCE == bulkstockSource && BulkstockSourceEnum.FINANCE != fbe.getBulkstockSource()) {
				throw new BusinessException("[融资]货物信息不允许与其他来源的货物合并下达收货通知");
			} else if (BulkstockSourceEnum.CREDIT == bulkstockSource && !replenishCreditId.equals(fbe.getReplenishCreditId())) {
				throw new BusinessException("[补货]货物信息不允许与其他来源的货物合并下达收货通知");
			} else if (BulkstockSourceEnum.PICKUP == bulkstockSource && !exchangePickupId.equals(fbe.getExchangePickupId())) {
				throw new BusinessException("[换货]货物信息不允许与其他来源的货物合并下达收货通知");
			}
		}
		recieveNoticeEntity.setBulkstockSource(bulkstockSource);
		recieveNoticeEntity.setExchangePickupId(exchangePickupId);
		recieveNoticeEntity.setReplenishCreditId(replenishCreditId);
		return recieveNoticeEntity;
	}

	public static RecieveNoticeDetail toRecieveNoticeDetail(RecieveNoticeEntity entity) {
		FinanceContractService financeContractService = ComponentFactory.loadClass(FinanceContractService.class);
		RecieveNoticeDetail detail = new RecieveNoticeDetail();
		BeanUtils.copyProperties(entity, detail);
		detail.setNoticeState(entity.getNoticeState().ordinal());
		detail.setProductType(entity.getProductType().ordinal());
		detail.setEnterpriseName(entity.getEnterpriseName());
		FinanceContractEntity fce = financeContractService.inquireFinanceIdContract(entity.getFinanceId(), 2);
		if (fce != null) {
			detail.setFinanceContractSerialNumber(fce.getFinanceContractSerialNumber());
		}
		RecieveNoticeInventoryService recieveNoticeDetailService = ComponentFactory.loadClass(RecieveNoticeInventoryService.class);
		List<RecieveNoticeInventoryEntity> detailEntities = recieveNoticeDetailService.inquireRecieveNoticeInventoryList(entity.getRecieveNoticeId());
		for (RecieveNoticeInventoryEntity de : detailEntities) {
			RecieveNoticeInventory bulkstock = new RecieveNoticeInventory();
			BeanUtils.copyProperties(de, bulkstock);
			bulkstock.setRecieveNoticeDetailId(de.getNoticeInventoryId());
			detail.getBulkstocks().add(bulkstock);
		}
		return detail;
	}

	public static RecieveNoticeLineInfo toRecieveNoticeLineInfo(RecieveNoticeEntity entity) {
		RecieveNoticeLineInfo info = new RecieveNoticeLineInfo();
		BeanUtils.copyProperties(entity, info);
		info.setNoticeState(entity.getNoticeState().ordinal());
		return info;
	}

	// =====================================================================================================================================================//
	/** 运输通知 */
	public static TransportNoticeEntity fromTransportNotice(Long financeId, String financeNumber, Long logisticsCompanyId, String logisticsCompanyName, String noticeRemark) {
		TransportNoticeEntity entity = new TransportNoticeEntity();
		entity.setFinanceId(financeId);
		entity.setFinanceNumber(financeNumber);
		entity.setLogisticsCompanyId(logisticsCompanyId);
		entity.setLogisticsCompanyName(logisticsCompanyName);
		entity.setNoticeRemark(noticeRemark);
		return entity;
	}

	public static void completeToTransportNoticeLineInfo(TransportNotice info, TransportNoticeEntity entity) {
		FinanceApplicationService financeApplicationService = ComponentFactory.loadClass(FinanceApplicationService.class);
		FinanceApplicationEntity finance = financeApplicationService.inquireFinanceApplicationEntity(entity.getFinanceId());
		BeanUtils.copyProperties(entity, info);
		info.setNoticeState(entity.getNoticeState().ordinal());
		if (finance != null) {
			info.setDeliverAddress(finance.getDeliverProvince() + finance.getDeliverCity() + finance.getDeliverDistrict() + finance.getDeliverAddress());
			info.setReceiveAddress(finance.getReceiveProvince() + finance.getReceiveCity() + finance.getReceiveDistrict() + finance.getReceiveAddress());
			info.setProductType(finance.getProductType().ordinal());
			info.setEnterpriseName(finance.getEnterpriseName());
		}
	}

	public static TransportNoticeDetail toTransportNoticeDetail(TransportNoticeEntity entity) {
		TransportNoticeDetail info = new TransportNoticeDetail();
		completeToTransportNoticeLineInfo(info, entity);
		ContainerInventoryService containerInventoryService = ComponentFactory.loadClass(ContainerInventoryService.class);
		List<ContainerInventoryEntity> inventoryEntityList = containerInventoryService.inquireTransportContainerInventoryList(entity.getTransportNoticeId());
		for (ContainerInventoryEntity cie : inventoryEntityList) {
			info.getContainerInventorys().add(BulkstockCastor.toContainerInventory(cie, Boolean.FALSE));
		}
		return info;
	}

	public static TransportNoticeLineInfo toTransportNoticeLineInfo(TransportNoticeEntity entity) {
		TransportNoticeLineInfo info = new TransportNoticeLineInfo();
		completeToTransportNoticeLineInfo(info, entity);
		FinanceContractService financeContractService = ComponentFactory.loadClass(FinanceContractService.class);
		FinanceContractEntity fce = financeContractService.inquireFinanceIdContract(entity.getFinanceId(), 2);
		if (fce != null) {
			info.setFinanceContractSerialNumber(fce.getFinanceContractSerialNumber());
		}
		ContainerBulkstockService containerBulkstockService = ComponentFactory.loadClass(ContainerBulkstockService.class);
		List<ContainerBulkstockEntity> containerBulkstockList = containerBulkstockService.inquireTransportContainerBulkstockList(entity.getTransportNoticeId());
		BulkstockCastor.fillBulkstockInventories(containerBulkstockList, info.getBulkstockInventories());
		return info;
	}

	// =====================================================================================================================================================//
	/** 放货通知 */
	public static DeliveryNoticeEntity fromDeliveryNotice(PickupApplicationEntity pickupEntity, Long logisticsCompanyId, String noticeRemark) {
		DeliveryNoticeEntity deliveryNoticeEntity = new DeliveryNoticeEntity();
		deliveryNoticeEntity.setPickupId(pickupEntity.getPickupId());
		deliveryNoticeEntity.setPickupNumber(pickupEntity.getPickupNumber());
		deliveryNoticeEntity.setFinanceId(pickupEntity.getFinanceId());
		deliveryNoticeEntity.setFinanceNumber(pickupEntity.getFinanceNumber());
		deliveryNoticeEntity.setDeliveryDate(pickupEntity.getDeliveryDate());

		LogisticsCompanyService logisticsCompanyService = ComponentFactory.loadClass(LogisticsCompanyService.class);
		LogisticsCompanyEntity logisticsCompanyEntity = logisticsCompanyService.inquireLogisticsCompanyDetail(logisticsCompanyId);
		deliveryNoticeEntity.setLogisticsCompanyId(logisticsCompanyEntity.getCompanyId());
		deliveryNoticeEntity.setLogisticsCompanyName(logisticsCompanyEntity.getCompanyName());

		deliveryNoticeEntity.setNoticeRemark(noticeRemark);
		return deliveryNoticeEntity;
	}

	private static void completeToDeliveryNoticeLineInfo(DeliveryNoticeLineInfo info, DeliveryNoticeEntity entity) {
		BeanUtils.copyProperties(entity, info);
		info.setNoticeState(entity.getNoticeState().ordinal());
		info.setDeliveryDate(DateUtil.formatDate(entity.getDeliveryDate()));
		FinanceApplicationService financeApplicationService = ComponentFactory.loadClass(FinanceApplicationService.class);
		FinanceApplicationEntity finance = financeApplicationService.inquireFinanceApplicationEntity(entity.getFinanceId());
		if (finance != null) {
			info.setReceiveAddress(finance.getReceiveAddress());
			info.setProductType(finance.getProductType().ordinal());
			info.setEnterpriseName(finance.getEnterpriseName());
		}

		PickupBulkstockService pickupBulkstockService = ComponentFactory.loadClass(PickupBulkstockService.class);
		List<PickupBulkstockEntity> bulkstockEntities = pickupBulkstockService.inquirePickupBulkstockList(entity.getPickupId());
		BulkstockCastor.completeToPickupBulkstockList(bulkstockEntities, info.getPickupBulkstocks());
	}

	public static DeliveryNoticeDetail toDeliveryNoticeDetail(DeliveryNoticeEntity entity) {
		DeliveryNoticeDetail detail = new DeliveryNoticeDetail();
		completeToDeliveryNoticeLineInfo(detail, entity);

		ContainerInventoryService containerInventoryService = ComponentFactory.loadClass(ContainerInventoryService.class);
		List<ContainerInventoryEntity> containerInventoryList = containerInventoryService.inquirePickupContainerInventoryList(entity.getPickupId());
		for (ContainerInventoryEntity cie : containerInventoryList) {
			detail.getContainerInventorys().add(BulkstockCastor.toContainerInventory(cie, Boolean.FALSE));
		}
		return detail;
	}

	public static DeliveryNoticeLineInfo toDeliveryNoticeLineInfo(DeliveryNoticeEntity entity) {
		DeliveryNoticeLineInfo info = new DeliveryNoticeLineInfo();
		completeToDeliveryNoticeLineInfo(info, entity);
		FinanceContractService financeContractService = ComponentFactory.loadClass(FinanceContractService.class);
		FinanceContractEntity fce = financeContractService.inquireFinanceIdContract(entity.getFinanceId(), 2);
		if (fce != null) {
			info.setFinanceContractSerialNumber(fce.getFinanceContractSerialNumber());
		}
		return info;
	}

}
